import re
from PIL import Image
import numpy as np

PIC_FILE_PATH = "C:/Users/GMZ/Desktop/本学期资料/学校课程/嵌入式系统/实验课/air-craft-on-ucos-iii/air-craft-on-ucos-iii/air-craft-on-ucos-iii/PixArrayGenerator/recources/blue_win.png"
searchObj = re.search( r'/(\w*?).\w{3}$', PIC_FILE_PATH)
file_name = searchObj.group(1)
imarray = np.array(Image.open(PIC_FILE_PATH))

with open('C:/Users/GMZ/Desktop/a/'+file_name+'.txt', 'w') as f:
    f.write("Shape: [{}][{}]\n".format(imarray.shape[0], imarray.shape[1]))
    f.write("extern const uint16_t "+ file_name.upper() +"_PixArray["+file_name.upper()[:-1]+"IMG_SIZE_Y]["+file_name.upper()[:-1]+"IMG_SIZE_X];\n")
    a = "const uint16_t "+ file_name.upper() +"_PixArray["+file_name.upper()[:-1]+"IMG_SIZE_Y]["+file_name.upper()[:-1]+"IMG_SIZE_X] = {\n"
    f.write(a)
    for i in imarray:
        f.write('\t{')
        for j in i:
            rgb565 = int((j[0] & 0xf8) * pow(2,8) + (j[1] & 0xfc) * pow(2,3) + (j[2] & 0xf8) / pow(2,3))  # rgb888 to rgb565
            f.write("0x{:04X}, ".format(rgb565))
        f.write("},\n")
    f.write("};\n")
    print(file_name + '.txt Generate Successfully!')
